package com.wzy.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wzy.reggie.common.CustomException;
import com.wzy.reggie.dto.DishDto;
import com.wzy.reggie.dto.SetmealDto;
import com.wzy.reggie.entity.*;
import com.wzy.reggie.mapper.SetmealMapper;
import com.wzy.reggie.service.CategoryService;
import com.wzy.reggie.service.SetmealDishService;
import com.wzy.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    @Lazy//利用延迟加载解决循环依赖（SetmealServiceImpl和CategoryServiceImpl彼此循环依赖）
    private CategoryService  categoryService;
    /**
     * 新增套餐，同时需要保存套餐和菜品的关联关系
     * @param setmealDto
     */
    @Override
    @Transactional//操作俩张表，开启事务 保证数据一致性 要么同时成功 要么同时失败
    public void saveWithDish(SetmealDto setmealDto) {
        //保存套餐基本信息 操作setmeal_dish 执行insert操作
        this.save(setmealDto);
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes.stream().map((item)->{
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());

        //保存套餐和菜品的关联信息 操作setmeal_dish 执行insert操作
        setmealDishService.saveBatch(setmealDishes);
    }

    /**
     * s删除套餐 同时删除套餐和菜品的关联数据
     * @param ids
     */
    @Override
    @Transactional
    public void removeWithDish(List<Long> ids) {
        //查询套餐的状态 确定是否可以删除 select cont(*) from setmeal where id in (1,2,3) and status = 1
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(Setmeal::getId,ids);
        queryWrapper.eq(Setmeal::getStatus,1);
        int count = this.count(queryWrapper);
        if(count>0){ //如果不可以删除，抛出一个异常
            throw new CustomException("有套餐正在售卖中，不能删除");
        }
       //如果可以删除，先删除套餐表中的数据---setmeal
        this.removeByIds(ids);

        //然后删除关系表中的数据--setmeal_dish
       // delete from setmeal_dish where setmeal_id in (1,2,3...)
        LambdaQueryWrapper<SetmealDish> setmealDishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealDishLambdaQueryWrapper.in(SetmealDish::getSetmealId,ids);
        setmealDishService.remove(setmealDishLambdaQueryWrapper);
    }

    /**
     * 根据id查询套餐信息和 对应的菜品信息
     * @param id
     * @return
     */
    @Override
    public SetmealDto getByIdWithFlavor(Long id) {
        //查询套餐基本信息，从setmeal表查询
        Setmeal setmeal = this.getById(id);//查询出套餐

        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(setmeal,setmealDto);

        //查询当前套餐对应的菜品信息，从setmeal_dish表查询
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId,id);
        List<SetmealDish> list = setmealDishService.list(queryWrapper);

        setmealDto.setSetmealDishes(list);
        return setmealDto;
    }

    /**
     * 修改套餐提交
     * @param setmealDto
     */
    @Override
    public void updateWithFlavor(SetmealDto setmealDto) {
        //先根据前端传的分类id查出分类名字
        Long categoryId = setmealDto.getCategoryId();
        Category category = categoryService.getById(categoryId);
        setmealDto.setCategoryName(category.getName());
        //因为涉及到俩张表 需要更新俩张表
        //1.更新套餐表
        this.updateById(setmealDto);


        //清理当前套餐对应的菜品数据--setmeal_dish表的delete操作
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId,setmealDto.getId());
        setmealDishService.remove(queryWrapper);

        //插入当前提交的菜品数据--setmeal_dish表的insert操作
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();

        setmealDishes = setmealDishes.stream().map((item)->{
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());
        setmealDishService.saveBatch(setmealDishes);
    }
}
